package com.cuit.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cuit.mapper.LetterMapper;
import com.cuit.mapper.ReflexMapper;
import com.cuit.mapper.UserMapper;
import com.cuit.pojo.Letter;
import com.cuit.pojo.Reflex;
import com.cuit.pojo.User;
import com.cuit.result.R;
import com.cuit.service.FileService;
import com.cuit.service.LetterService;
import com.cuit.service.ReflexService;
import com.cuit.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.management.ValueExp;
import java.sql.Ref;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Optional;

@Api(tags = "留言")
@CrossOrigin
@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private LetterService letterService;

    @Autowired
    private LetterMapper letterMapper;

    @Autowired
    private ReflexMapper reflexMapper;

    @Autowired
    private ReflexService reflexService;

    @Autowired
    private FileService fileService;

    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;

    //根据用户名查询所有留言
    @RequestMapping("/MessageByUsername/{username}")
    @ApiOperation("根据用户名查询所有留言")
    public R MessageByUsername(@PathVariable String username){
        QueryWrapper<Letter> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Letter::getNameto,username);
        List<Letter> list = letterMapper.selectList(queryWrapper);
        if (!list.isEmpty()){
            for(int i=0;i<list.size();i++){
                list.get(i).setFromavatar(fileService.allPath(list.get(i).getFromavatar()));
            }
        }
        return R.ok().data("list",list);
    }

    //根据用户名查询所有回复
    @RequestMapping("/ReplyByUsername/{username}")
    @ApiOperation("根据用户名查询所有回复")
    public R ReplyByUsername(@PathVariable String username){
        QueryWrapper<Reflex> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Reflex::getNameto,username);
        List<Reflex> list = reflexMapper.selectList(queryWrapper);
        if (!list.isEmpty()){
            for(int i=0;i<list.size();i++){
                list.get(i).setFromavatar(fileService.allPath(list.get(i).getFromavatar()));
            }
        }
        return R.ok().data("list",list);
    }

    //将用户留言插入letter表中
    @RequestMapping("/AddMessage")
    @ApiOperation("将用户留言插入message表中")
    public R AddMessage(@RequestBody Letter letter){
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        letter.setDate(sdf.format(System.currentTimeMillis()));
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getUsername,letter.getNamefrom());
        List<User> list = userMapper.selectList(queryWrapper);
        letter.setFromavatar(list.get(0).getAvatar());
        letterMapper.insert(letter);
        return R.ok();
    }

    //将回复插入reflex表中
    @RequestMapping("/AddReply")
    @ApiOperation("将用户留言插入reply表中")
    public R AddReply(@RequestBody Reflex reflex){
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        reflex.setDate(sdf.format(System.currentTimeMillis()));
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getUsername,reflex.getNamefrom());
        List<User> list = userMapper.selectList(queryWrapper);
        reflex.setFromavatar(list.get(0).getAvatar());
        reflexMapper.insert(reflex);
        Letter letter = letterMapper.selectById(reflex.getMid());
        letter.setState(1);
        letterMapper.updateById(letter);
        return R.ok();
    }

    //根据mid查询回复
    @RequestMapping("/reflexByMid/{mid}")
    @ApiOperation("根据mid查询回复")
    public R reflexByMid(@PathVariable String mid){
        QueryWrapper<Reflex> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(Reflex::getMid,mid);
        List<Reflex> list = reflexMapper.selectList(queryWrapper);
        if(list.isEmpty()){
            Reflex reflex = new Reflex();
            reflex.setDetail("");
            return R.ok().data("reflex",reflex);
        }
        else {
            return R.ok().data("reflex",list.get(0));
        }
    }

    //通过用户名获取用户的留言与回复总数
    @RequestMapping("/countMessage/{username}")
    @ApiOperation("通过用户名获取用户的留言与回复总数")
    public R countMessage(@PathVariable String username){
        int count = 0;
        QueryWrapper<Letter> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Letter::getNameto,username);
        List<Letter> list = letterMapper.selectList(queryWrapper);
        if(!list.isEmpty()){
            count = count+list.size();
        }
        else if (list.isEmpty()){
            count = 0;
        }
        QueryWrapper<Reflex> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.lambda().eq(Reflex::getNameto,username);
        List<Reflex> list1 = reflexMapper.selectList(queryWrapper1);
        if(!list1.isEmpty()){
            count = count+list1.size();
        }
        else if(list1.isEmpty()){
            count = count+0;
        }
        return R.ok().data("count",count);
    }

    //通过mid查找留言
    @RequestMapping("/ReflexByMid/{id}")
    @ApiOperation("通过mid查找留言")
    public R ReflexByMid(@PathVariable String id){
        QueryWrapper<Letter> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(Letter::getId,id);
        List<Letter> list = letterMapper.selectList(queryWrapper);
        String detail = list.get(0).getDetail();
        return R.ok().data("detail",detail);
    }

    //通过id删除留言
    @RequestMapping("/DelLetterById/{id}&{username}")
    @ApiOperation("通过id删除留言")
    public R DelLetterById(@PathVariable String id,@PathVariable String username){
        letterMapper.deleteById(id);
        QueryWrapper<Letter> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(Letter::getNameto,username);
        List<Letter> list = letterMapper.selectList(queryWrapper);
        return R.ok().data("list",list);
    }

    //通过id删除回复
    @RequestMapping("/DelReflexById/{id}&{username}")
    @ApiOperation("通过id删除回复")
    public R DelReflexById(@PathVariable String id,@PathVariable String username){
        reflexMapper.deleteById(id);
        QueryWrapper<Reflex> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(Reflex::getNameto,username);
        List<Reflex> list = reflexMapper.selectList(queryWrapper);
        return R.ok().data("list",list);
    }
}
